const db = wx.cloud.database()
const _ = db.command
var app = getApp()
Component({
	properties: {
		hasArea:Boolean,
		hasType:Boolean
	}, 

	/**
	 * 组件的初始数据
	 */
	data: {
		upData:{all:"全国",type:"全部类型"},
		typeData: ["全部类型","创新创业", "艺术", "体育", "计算机", "学科", "其他"],
		typeIndex: 0,
		teamData: {},
		areaData: [
			["全国"],
			["全省"],
			["全市"]
		],
		area: "全国",
		areaIndex: [0, 0, 0],
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		typeChange(e) {
			// console.log(e)
			this.setData({
				typeIndex: e.detail.value
			})
			//传至到页面
			let data = this.data.upData
			data.type = this.data.typeData[e.detail.value]
			this.setData({
				upData:data
			})
			this.triggerEvent('getPicker', data)
		},
		areaColumnChange(e) {
			// console.log(e)
			let column = e.detail.column
			let index = e.detail.value
			//all
			if (column == 0 && index == 0) {
				let area1 = "areaData[1]"
				let area2 = "areaData[2]"
				this.setData({
					[area1]: ["全省"],
					[area2]: ["全市"],
					areaIndex: [0, 0, 0]
				})
			}
			if (column == 1 && index == 0) {
				let areaData = "areaData[2]"
				let areaIndex = "areaIndex[2]"
				let areaIndex1 = "areaIndex[1]"
				this.setData({
					[areaIndex1]: 0,
					[areaIndex]: 0,
					[areaData]: ["全市"]
				})
			}
			//1
			if (column == 0 && index != 0) {
				wx.cloud.callFunction({
					name: "getCities",
					data: {
						province: this.data.areaData[0][index]
					}
				}).then(res => {
					// console.log(res)
					let data = res.result.data[0].cities
					// console.log(data)
					let citys = ["全省"]
					for (let i = 0; i < data.length; i++) {
						citys.push(data[i].city_name)
						// console.log(data[i])
					}
					console.log(citys)
					let citysData = "areaData[1]"
					let collegesData = "areaData[2]"
					this.setData({
						[citysData]: citys,
						areaIndex: [index, 0, 0],
						[collegesData]: ["全市"]
					})
				})
			}
			//2
			if (column == 1 && index != 0) {
				wx.cloud.callFunction({
					name: "getCities",
					data: {
						province: this.data.areaData[0][this.data.areaIndex[0]]
					}
				}).then(res => {
					// console.log(res)
					// console.log(this.data.areaData[1][index])
					let data = res.result.data[0].cities
					// console.log(data)
					let colleges = ["全市"]
					for (let i = 0; i < data.length; i++) {
						console.log(data[i].city_name)
						if (data[i].city_name == this.data.areaData[1][index]) {
							colleges = colleges.concat(data[i].universities)
							break
						}
						// console.log(this.data.areaData[1][index])
						// console.log(data[i])
					}
					console.log(colleges)
					let collegesData = "areaData[2]"
					let areaIndex1 = "areaIndex[1]"
					let areaIndex2 = "areaIndex[2]"
					this.setData({
						[collegesData]: colleges,
						[areaIndex1]: index,
						[areaIndex2]: 0
					})
				})
			}
		},
		areaChange(e) {
			let indexArr = e.detail.value
			this.setData({
				areaIndex: indexArr
			})
			if (indexArr[0] == 0) {
				this.setData({
					area: this.data.areaData[0][0]
				})
			} else if (indexArr[1] == 0) {
				this.setData({
					area: this.data.areaData[0][indexArr[0]]
				})
			} else if (indexArr[2] == 0) {
				this.setData({
					area: this.data.areaData[1][indexArr[1]]
				})
			} else {
				this.setData({
					area: this.data.areaData[2][indexArr[2]]
				})
			}
			//传至到页面
			let data = {}
			if (this.data.areaIndex[0] == 0) {
				data = {
					all: this.data.area,
					type: this.data.typeData[this.data.typeIndex]
				}
			} else if (this.data.areaIndex[1] == 0){
				data = {
					province: this.data.area,
					type: this.data.typeData[this.data.typeIndex]
				}
			} else if (this.data.areaIndex[2] == 0){
				data = {
					city: this.data.area,
					type: this.data.typeData[this.data.typeIndex]
				}
			}else{
				data = {
					school: this.data.area,
					type: this.data.typeData[this.data.typeIndex]
				}
			}
			this.setData({
				upData:data
			})
			this.triggerEvent('getPicker', data)
		},
	},
	///////////////
	lifetimes: {
		ready: function () {
	
		},
	},
	pageLifetimes: {
		show: function () {
			// 准备pick函数
			wx.cloud.callFunction({
				name: "getProvinces"
			}).then(res => {
				let data = res.result.data
				console.log(data)
				let provinces = []
				for (let i = 0; i < data.length; i++) {
					provinces.push(data[i].province_name)
				}
				console.log(provinces)
				let provinceData = "areaData[0]"
				this.setData({
					[provinceData]: ["全国"].concat(provinces)
				})
			})
		},
	}
})
